package com.ktgj.hotel.utils;

import javax.net.ssl.*;
import java.io.*;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.GZIPInputStream;

public class HttpsUtils {

    /**

     * https get请求

     * @param url

     * @return

     */

    public static String httpsGet(String url){

        String str_return = "";

        HttpsURLConnection conn = null;

        try {

            SSLContext sc = SSLContext.getInstance("SSL");

            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },

                    new java.security.SecureRandom());

            URL console = new URL(url);

            conn = (HttpsURLConnection) console.openConnection();

            conn.setSSLSocketFactory(sc.getSocketFactory());

            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());

            conn.setRequestProperty("Accept-Encoding","gzip");

            conn.connect();

            BufferedReader br = null;
            StringBuilder result = new StringBuilder();
            InputStream is = conn.getInputStream();

            InputStream in = new GZIPInputStream(is);
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line;
            while ((line = br.readLine()) != null)
            {
                result.append(line);
            }
            str_return =result.toString();
            br.close();
            is.close();

//            BufferedReader br = new BufferedReader(new InputStreamReader(is,  ("ISO-8859-1")));
//
//            String ret = "";
//
//            while (ret != null) {
//
//                ret = br.readLine();
//
//                if (ret != null && !ret.trim().equals("")) {
//
//                    str_return = str_return
//
//                            + new String(ret.getBytes("ISO-8859-1"), "utf-8");
//
//                }
//
//            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            conn.disconnect();

        }

        return str_return;

    }

    /**

     * https Post请求

     * @param url

     * @return

     */

    public static String httpsPost(String url, Map parameters){

        String str_return = "";

        HttpsURLConnection conn = null;

        try {

            StringBuffer params = new StringBuffer();

            for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();){

                Map.Entry element = (Map.Entry) iter.next();

                params.append(element.getKey().toString());

                params.append("=");

                params.append(URLEncoder.encode(element.getValue().toString(),"utf-8"));

                params.append("&");

            }

            if (params.length() > 0) {

                params = params.deleteCharAt(params.length() - 1);

            }

            SSLContext sc = SSLContext.getInstance("SSL");

            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },

                    new java.security.SecureRandom());

            URL console = new URL(url);

            conn = (HttpsURLConnection) console.openConnection();

            conn.setRequestMethod("POST");

            conn.setSSLSocketFactory(sc.getSocketFactory());

            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());

            conn.setDoOutput(true);

            byte[] b = params.toString().getBytes();

            conn.getOutputStream().write(b, 0, b.length);

            conn.getOutputStream().flush();

            conn.getOutputStream().close();

            conn.connect();

            InputStream is = conn.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(is,

                    ("ISO-8859-1")));

            String ret = "";

            while (ret != null) {

                ret = br.readLine();

                if (ret != null && !ret.trim().equals("")) {

                    str_return = str_return

                            + new String(ret.getBytes("ISO-8859-1"), "utf-8");

                }

            }

        } catch (ConnectException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();

        } catch (KeyManagementException e) {

            e.printStackTrace();

        } finally {

            conn.disconnect();

        }

        return str_return;

    }

////测试一下
//
//    public static void main(String[] args) throws Exception {
//
//        String string = HttpUtil2.httpsGet("https://www.baidu.com");
//
//        System.out.println(string);
//
//    }

}